package shared;

import java.io.Serializable;
import java.util.List;

import javax.xml.bind.annotation.*;

@SuppressWarnings("serial")
@XmlRootElement(name = "task")
public class Task implements Serializable {

	@XmlAttribute
	private String status;

	@XmlAttribute
	private String date;

	@XmlAttribute
	private String name;

	@XmlAttribute
	private String id;

	private String attendants;

	private String description;

	public Task() {

	}

	public Task(String status, String date, String name, String id,
			String attendants, String description) {
		this.status = status;
		this.date = date;
		this.name = name;
		this.id = id;
		this.attendants = attendants;
		this.description = description;
	}

	public Task(String status, String date, String name, String id,
			List<User> attendants, String description) {
		this.status = status;
		this.date = date;
		this.name = name;
		this.id = id;
		
		for(User attendant: attendants) {
			addAttendant(attendant);
		}
		
		this.description = description;
	}

	@XmlElement(name = "attendants")
	public String getAttendants() {
		return attendants;
	}

	public void setAttendants(String value) {
		attendants = value;
	}

	public void addAttendant(User attendant) {
		attendants += ", " + attendant.getName();
	}

	public void removeAttendant(User attendant) {
		// Get name from user
		String name = attendant.getName();

		// If the user is in the string
		if (attendants.contains(name)) {
			// Remove the name
			attendants.replaceAll(name, "");
			// Remove any unneeded commas
			attendants = attendants.replaceAll(", ", "");
		}
	}

	@XmlElement(name = "description")
	public String getDescription() {
		return description;
	}

	public void setDescription(String value) {
		description = value;
	}
	
	public String getStatus() {
		return status;
	}

	public String getDate() {
		return date;
	}

	public String getName() {
		return name;
	}

	public String getId() {
		return id;
	}
	
	public boolean equals(Object other) {
		if(this == other) {
			return true;
		} else if (other instanceof Task){
			Task otherTask = (Task) other;
			if(this.id == otherTask.getId()) {
				return true;
			}			
		}		
		return false;
	}
}